﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using DevExpress.XtraEditors;
using MicropleSoft.Controls.DialogBox;
using MicropleSoft.WmsApp.Library.Proxy;
using MicropleSoft.WmsApp.Library.Security;

namespace MicropleSoft.WmsApp.SecurityAuthentication
{
    public partial class FormChangePass : DevExpress.XtraEditors.XtraForm
    {
        private bool _isResetPassword = false;
        private readonly ServerProxy _serverProxy = ServerProxyFactory.GetServerProxy();
        public FormChangePass()
        {
            InitializeComponent();
        }

        public FormChangePass(bool isResetPassword)
        {
            InitializeComponent();
            _isResetPassword = isResetPassword;
            this.textEditOld.Enabled = false;
        }

        private bool ValidatePassword()
        {
            if (textEditNew1.Text != textEditNew2.Text || String.IsNullOrEmpty(textEditNew1.Text))
            {
                XtraMessageBox.Show("两次新密码输入不一致！或者为空！", "错误", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                return false;
            }
            return true;
        }

        private void CmdOk_Click(object sender, EventArgs e)
        {
            try
            {
                bool b = ValidatePassword();
                if (!b)
                {
                    return;
                }

                object result;
                if (_isResetPassword)
                {
                    result = _serverProxy.CallService("SetPassword", UserSession.CurrentUser.UserId, this.textEditNew1.Text);
                }
                else
                {
                    result = _serverProxy.CallService("ChangePassword", UserSession.CurrentUser.UserId,
                                                      this.textEditOld.Text, this.textEditNew1.Text);
                }
                CustomMessageBox.Show(result.ToString(), CustomMessageBox.MsgBoxIcons.Info,
                                      CustomMessageBox.MsgBoxIcons.Info);

            }
            catch (Exception exception)
            {
                Base.Exceptions.ExceptionHandler.ShowException(exception);
            }
        }
    }
}